package com.huobao.yunying.activitys;

import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.UserSetting;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class Act_PianHao_SheZhi extends HuobaoBaseActivity implements OnClickListener{

	@ViewInject(R.id.llyt_manager_photo)
	private View llyt_manager_photo;
	@ViewInject(R.id.img_userphoto)
	private ImageView img_userphoto;
	@ViewInject(R.id.imgView)
	private ImageView imgView;
	@ViewInject(R.id.tv_name)
	private TextView tv_name;
	@ViewInject(R.id.tv_tel)
	private TextView tv_tel;
	//修改资料
	@ViewInject(R.id.llyt_ziliao)
	private View llyt_ziliao;
	//收货地址
	@ViewInject(R.id.llyt_address)
	private View llyt_address;
	//密码管理
	@ViewInject(R.id.llyt_manager_pwd)
	private View llyt_manager_pwd;
	//rexian 
	@ViewInject(R.id.llyt_rexian)
	private View llyt_rexian;
	
	//通用
	@ViewInject(R.id.llyt_tongyong)
	private View llyt_tongyong;

	@Override
	protected int getContentView() {
		return R.layout.layout_pianhaoshezhi;
	}

	@Override
	protected String getTitleText() {
		return "偏好设置";
	}

	@Override
	protected void init() {
		EventBus.getDefault().register(this);
		llyt_ziliao.setOnClickListener(this);
		llyt_manager_photo.setOnClickListener(this);
		llyt_address.setOnClickListener(this);
		llyt_manager_pwd.setOnClickListener(this);
		llyt_rexian.setOnClickListener(this);
		llyt_tongyong.setOnClickListener(this);
		getData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	public void onLoginOut(View view) {
		SessionUtils.loginout(context);
		EventBus.getDefault().post("", "goFristPage");
		Intent intent = new Intent(this, Act_Login.class);
		startActivity(intent);
		finish();
	}

	private UserSetting setting;
	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "member", "act", "setting");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				setting = JSON.parseObject(jsonObject.getString("retval"), UserSetting.class);
				show(setting);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	private String user_id;
	protected void show(UserSetting setting) {
		user_id = setting.user_id;
		ImageUtils.loadimg(imgView, setting.logo);
		ImageUtils.loadimg(img_userphoto, setting.portrait);
		tv_name.setText(setting.user_name);
		tv_tel.setText(setting.hot_line);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.llyt_ziliao://修改资料
			Intent intent = new Intent(this, Act_Edit_Msg.class);
			startActivity(intent);
			break;
		case R.id.llyt_manager_photo:
			intent = new Intent(this, Act_Manager_UserPhoto.class);
			intent.putExtra("user_id", user_id);
			startActivity(intent);
			break;
		case R.id.llyt_address:
			intent = new Intent(this, Act_Manager_Address.class);
			startActivity(intent);
			break;
		case R.id.llyt_manager_pwd:
			intent = new Intent(this, Act_Manager_Pwd.class);
			startActivity(intent);
			break;
		case R.id.llyt_rexian:
			Uri uri = Uri.parse("tel:" + setting.hot_line);
			intent = new Intent(Intent.ACTION_DIAL, uri);
			startActivity(intent);
			break;
		case R.id.llyt_tongyong:
			intent = new Intent(this, Act_Tongyong.class);
			startActivity(intent);
			break;
		}
	}
	
	@Subscriber(tag = "uploadPhotoSuccess")
	public void uploadPhotoSuccess(String photo){
		ImageUtils.loadimg(img_userphoto, photo);
	}
}
